from django.db import models
from django.contrib.auth.models import User


class UserProfile(models.Model):
    user = models.OneToOneField(
        User,
        on_delete=models.CASCADE,
        null=True,
        verbose_name="user",
    )
    name = models.CharField(
        "姓名",
        max_length=20,
        null=True,
    )
    phonenumber = models.CharField(
        "电话号码",
        max_length=20,
        null=True,
    )

    def __str__(self):
        return f"#{self.user.id} " \
               f"{self.name} " \
               f"{self.phonenumber}"

    class Meta:
        verbose_name = "用户档案"
        verbose_name_plural = verbose_name


class ImageRecognition(models.Model):
    create_at = models.DateTimeField(auto_now_add=True)
    name = models.CharField(max_length=200, unique=True)
    img_type = models.CharField(max_length=30)
    res_json = models.TextField(null=True)
    task_id = models.CharField(max_length=100)

    def __str__(self):
        return f"#{self.id} {self.name}"

    class Meta:
        verbose_name = "图片识别结果"
        verbose_name_plural = verbose_name


class QandA(models.Model):
    create_at = models.DateTimeField(auto_now_add=True)
    task_id = models.CharField(max_length=50)
    question = models.TextField(null=False)
    imgs = models.ManyToManyField(ImageRecognition)
    status = models.CharField(default="waiting", max_length=20, null=True)
    answer = models.TextField(null=True)

    # meta data in Llama Index

    # input data

    in_physiologic_index = models.BooleanField(default=False)
    # eg. “帮我查询最近一周的体温记录”

    in_case_history = models.BooleanField(default=False)
    # eg. “我有哪些既往病史？”

    in_pharmacopoeia_western = models.BooleanField(default=False)
    # eg. “布洛芬缓释胶囊是什么？”

    in_pharmacopoeia_traditional_chinese = models.BooleanField(default=False)
    # eg. “连花清瘟胶囊是什么？”

    in_medical_handbook = models.BooleanField(default=False)
    # eg. “腹痛怎么办？”

    # output data

    out_symptom = models.BooleanField(default=False)
    # eg. “我可能感冒了，有些头晕发烧”

    out_physiologic_index = models.BooleanField(default=False)
    # eg. “我现在的血糖指数是8.9”

    out_medicine = models.BooleanField(default=False)
    # eg. “我今天服用了一次抗生素类药物”

    def __str__(self):
        return f"#{self.id} q={self.question[:20]} | a={self.answer[:20] if self.answer else 'null'}"

    class Meta:
        verbose_name = "问题与回答"
        verbose_name_plural = verbose_name
